﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using PeiYue.IServices.Tms;
using PeiYue.Model;
using PeiYue.Model.Dto.Tms;
using PeiYue.Common.HttpContextUser;

namespace PeiYue.Api.Controllers.Driver
{
    /// <summary>
    /// 司机退货管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class BackController : ControllerBase
    {
        readonly IDriverBackRecordServices _driverBackRecordServices;
        readonly IUser _user;
        public BackController(IDriverBackRecordServices driverBackRecordServices, IUser user)
        {
            _driverBackRecordServices = driverBackRecordServices;
            _user = user;
        }
        /// <summary>
        ///   获取退货的客户列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<List<CustomerList>>> GetCustomerList()
        {
            var result = await _driverBackRecordServices.GetCustomerList(_user.ResId);
            var list = result.Where(t=>t.Qty>0).GroupBy(t => new { t.CustomerCode, t.CustomerName }).Select(t => new CustomerList()
            {
                CustomerCode = t.Key.CustomerCode,
                CustomerName = t.Key.CustomerName,
                SkuList = t.Select(p => new SkuInfo() { SkuName = p.SkuName, SkuCode = p.SkuCode, Qty = p.Qty }).ToList()
            }).ToList();

            return new MessageModel<List<CustomerList>>()
            {
                msg = "获取成功",
                success = true,
                response = list
            };
        }
        /// <summary>
        ///   添加退货记录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<string>> AddRecord([FromBody] UpdateBackRecordRequestDto dto)
        {
            var list = await _driverBackRecordServices.AddBackRecord(dto,_user.ResId);
            return new MessageModel<string>()
            {
                msg = "成功",
                success = true,
            };
        }
    }
}